package com.laboratory.servicecustomer.setting.controller;

import com.Laboratory.setting.service._projectService;
import com.Laboratory.utilPojo.projectWithStateAndTime;
import com.Laboratory.utilPojo.projectWithUrl;
import com.Laboratory.workBench.pojo.project;
import com.Laboratory.workBench.pojo.teacher;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laboratory.servicecustomer.constant.constantArgs;
import com.laboratory.servicecustomer.util.fileUtil;
import com.laboratory.servicecustomer.utilPojo.comm;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author acok
 * @description:TODO
 * @Date 2023/1/4 16:31
 * @Version 1.0
 */
@Controller
@RequestMapping("/_project")
public class _projectController {
    @DubboReference(interfaceClass = _projectService.class, version = "1.0.0", timeout = 5000)
    private _projectService _projectService;

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/getProjectListByCondition")
    @ResponseBody
    public Object getProjectListByCondition(
            @RequestParam(name = "projectName", required = false) String projectName,
            @RequestParam(name = "projectState", required = false) String projectState,
            @RequestParam(name = "currentPage") Integer pageNo,
            @RequestParam(name = "pageSize") Integer pageSize
    ) {
        comm comm = new comm();
        Map<String, Object> map = new HashMap<>();
        map.put("projectName", projectName);
        map.put("projectState", projectState);
        map.put("pageNo", (pageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        try {
            Map<String, Object> resultMap = new HashMap<>();
            List<projectWithStateAndTime> projects = _projectService.getProjectListByCondition(map);
            int rows = _projectService.getProjectListRowsByCondition(map);
            resultMap.put("projects", projects);
            resultMap.put("rows", rows);
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/updateProjectById")
    @ResponseBody
    public Object updateProjectById(
            @RequestParam("projectObj") String project,
            @RequestParam("email") String email,
            @RequestParam("passwordCode") String passwordCode
    ) {
        comm comm = new comm();
        try {
            projectWithStateAndTime projectWithStateAndTime = JSON.parseObject(project, new TypeReference<projectWithStateAndTime>() {
            });
            if (_projectService.updateProjectState(projectWithStateAndTime, email, passwordCode) > 0) {
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            } else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @GetMapping("/getProjectFileList")
    @ResponseBody
    public Object getProjectFileList() {
        comm comm = new comm();
        try {
            List<projectWithUrl> urls = _projectService.getProjectFileList();
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(urls);
        } catch (Exception e) {
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @GetMapping("/downloadFileByProject")
    @ResponseBody
    public void downLoadFileByProject(
            @RequestParam("url") String url,
            HttpServletResponse response) {
        HSSFWorkbook wb = null;
        OutputStream outWeb = null;
        try {
            FileInputStream fi = new FileInputStream(url);
            wb = new HSSFWorkbook(fi);
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=Statistics.xls");
            outWeb = response.getOutputStream();
            wb.write(outWeb);
            wb.close();
            outWeb.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fileUtil.closeFileStream(outWeb, wb);
        }
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @GetMapping("/getInvalidProjectApplyList")
    @ResponseBody
    public Object getInvalidProjectApplyList() {
        comm comm = new comm();
        try {
            List<project> projects = _projectService.selectInvalidProjectApply();
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(projects);
        } catch (Exception e) {
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/clearProjectMessageById")
    @ResponseBody
    public Object clearProjectMessageById(@RequestParam("id") String id) {
        comm comm=new comm();
        try {
            if(_projectService.clearProjectMessageById(id)==4){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @GetMapping("/getOverTimeProjectRecord")
    @ResponseBody
    public Object getOverTimeProjectRecord(){
        comm comm=new comm();
        try {
            List<project> projects = _projectService.selectOverTimeProjectRecord();
            comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
            comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            comm.setOther(projects);
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"admin","Root"},logical = Logical.OR)
    @RequiresPermissions(value = {"admin:applyManage","Root:highest"},logical = Logical.OR)
    @PostMapping("/clearOverTimeProjectById")
    @ResponseBody
    public Object clearOverTimeProjectById(@RequestParam("id") String id) {
        comm comm=new comm();
        try {
            if(_projectService.clearOverTimeProjectRecord(id)==4){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"Root"})
    @RequiresPermissions(value = {"Root:highest"})
    @PostMapping("/removeTeacherById")
    @ResponseBody
    public Object removeTeacherById(@RequestParam("id") String id) {
        comm comm=new comm();
        try {
            if(_projectService.deleteTeacherById(id)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }

    @RequiresRoles(value = {"Root"})
    @RequiresPermissions(value = {"Root:highest"})
    @PostMapping("/createTeacher")
    @ResponseBody
    public Object createTeacher(@RequestBody teacher teacher) {
        comm comm=new comm();
        try {
            if(_projectService.createTeacher(teacher)>0){
                comm.setCode(constantArgs.COMMUNITY_SUCCESS_CODE);
                comm.setMessage(constantArgs.COMMUNITY_SUCCESS_MESSAGE);
            }else {
                comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
                comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
            comm.setCode(constantArgs.COMMUNITY_FAIL_CODE);
            comm.setMessage(constantArgs.COMMUNITY_FAIL_MESSAGE);
        }
        return comm;
    }
}

